"""

捕获异常
"""

# 基本捕获语法
# try:
#     f = open("D:/Python测试/abc.txt", 'r', encoding='UTF-8')
# except:
#     print("出现异常，文件不存在，open模式改为写入")
#     f = open("D:/Python测试/abc.txt", 'w', encoding='UTF-8')

# 捕获指定的异常
# try:
#     print(name)
#     # 1 / 0
#
# except NameError as e:
#     print("出现变量未定义")
#     print(e)

# 捕获多个异常
# try:
#     # 1 / 0
#     print(name)
# except (NameError, ZeroDivisionError) as e:
#     print("出现了变量为定义或者除以0的异常错误", e)

# 未正确设置捕获异常类型，将无法捕获异常

# 捕获所有异常
try:
    # 1 / 0
    # print(name)
    f = open("D:/abc.txt", 'r', encoding="UTF-8")
    print("hello")
except Exception as e:
    print("出现异常了", e)
    f = open("D:/abc.txt", 'w', encoding="UTF-8")
else:
    print("没有异常执行")
finally:
    print("finally，有无异常都会执行")
    f.close()
